package com.nstar.countdown.view.main.impl
{
	import com.adobe.serialization.json.JSON;
	import com.nstar.base.controls.view.LoggerView;
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.event.NavigateEvent;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.StringUtils;
	import com.nstar.countdown.ApplicationFacade;
	import com.nstar.countdown.view.components.TimeLabel;
	import com.nstar.countdown.view.main.Advice;
	import com.nstar.hacocwebfx.view.components.*;
	
	import flash.events.MouseEvent;
	import flash.utils.setInterval;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.ToggleButtonBar;
	import mx.events.ChildExistenceChangedEvent;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class HeaderClass extends Canvas
	{
		public var lblUser:Label;
		public var btnLogin:Button;
		public var btnRegister:Button;
		public var btnAdvice:Button;
		public var cboStatus:ComboBox;
		public var btnLog:Button;
		public var imgLogo:Image;
		public var tbbMenus:ToggleButtonBar;
		public var tlTime:TimeLabel;
		
		public var countDown:RemoteObject;
		
		[Bindable]public var menus:ArrayCollection = new ArrayCollection();
		public var module:IModuleInfo;
		
		public var logger:Logger = Logger.getInstance(this);
		[Bindable]
		public var aryMenus:ArrayCollection = new ArrayCollection(); 
				
		public function HeaderClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		
		private function init(event:FlexEvent):void{
			load();
			
			btnLogin.addEventListener(MouseEvent.CLICK,login);
			btnRegister.addEventListener(MouseEvent.CLICK,registerHandler);
			btnAdvice.addEventListener(MouseEvent.CLICK,adviceHandler);
			btnLog.addEventListener(MouseEvent.CLICK,showLogger);
			tbbMenus.addEventListener(ItemClickEvent.ITEM_CLICK,itemclickHandler);
			countDown.addEventListener(ResultEvent.RESULT,resultHandler);
			countDown.addEventListener(FaultEvent.FAULT,faultHandler);
			
		}
		
		private function login(event:MouseEvent):void{
			var lp:LoginPanel = LoginPanel.show();
			lp.addEventListener("login",loginHandler);
		}
		
		private function registerHandler(event:MouseEvent):void{
			
		}
		
		private function adviceHandler(event:MouseEvent):void{
			Advice.show();
		}
		
		private function showLogger(event:MouseEvent):void{
			LoggerView.show(logger.text,btnLog); 
		}
		
		private function itemclickHandler(event:ItemClickEvent):void{
			this.dispatchEvent(event);
		}
		
		private function resultHandler(event:ResultEvent):void{
			var obj:* = JSON.decode(event.result.toString());
			logger.debugMore("result",obj);
		}
		
		private function faultHandler(event:FaultEvent):void{
			logger.debugMore("fault",event.fault);
		}
		
		
		private function loginHandler(event:AdvanceEvent):void{
			
		}
		
		private function load():void{
			var menus:ArrayCollection = ApplicationFacade.getMainMenuProxy().getValue("result");
			tbbMenus.dataProvider = menus;
		}
		
		
		//菜单导航
		private function navigateHandler(event:NavigateEvent):void{
			loadModule(event.parameters.url);
		}
		
		//加载模块
		private function loadModule(moduleName:String):void{
			if(StringUtils.isEmpty(moduleName)) return ;
			
			module = ModuleManager.getModule(moduleName);
			module.addEventListener(ModuleEvent.READY,moduleHandler);
			module.addEventListener(ModuleEvent.PROGRESS,moduleHandler);
			module.addEventListener(ModuleEvent.UNLOAD,moduleHandler);
			module.addEventListener(ModuleEvent.ERROR,moduleHandler);
			
			module.load();
		}
		
		//模块加载事件处理
		private function moduleHandler(event:ModuleEvent):void{
			switch (event.type){
				case ModuleEvent.READY:
					ModuleLoaderProgress.hide();
					break;
				case ModuleEvent.PROGRESS:
					ModuleLoaderProgress.show();
					break;
				case ModuleEvent.UNLOAD:
					break;
				case ModuleEvent.ERROR:
					throw new Error(event.errorText);
					break;
			} 
		}			
		
	
		
		private function addChildHandler(event:ChildExistenceChangedEvent):void{
			event.relatedObject.width = 90;
			event.relatedObject.height = 25;
        }	
        
        public function playEffect():void{
        	
        }	
		
	}
}